<?php
/**
 * Global Report
 */
use Fox\Ddic\DataElement\Formater\Number;
use Fox\Grid\FieldCat\Field;
use Fox\Html\Interval;
use Fox\Html\DropList;
use Doctrine\DBAL\Connection;
use Fox\Html\Screen;
$screen = new Screen('eCriteria','vlook');
$block = new Fox\Html\Block('bCrieria',Msg(Fox::MSGCL,29));
$block->add(new Interval(array('period','date')));
$block->add(new DropList('timeframe',true,true,true));
$screen->add($block);
if($screen->submitted())
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		switch ($inputs['timeframe'])
		{
			case 'day' :
				$formatType = 'd/m/Y';
				$formatInterval = 'P1D';
			break;
			case 'week':
				$formatType = 'W/Y';
				$formatInterval = 'P1W';
			break;
			case 'month':
				$formatType = 'm/Y';
				$formatInterval = 'P1M';
			break;
			case 'year':
				$formatType = 'Y';
				$formatInterval = 'P1Y';
			break;
		}

		$fragments[] = $block->item('date')->fragments();
		$handlerkey = array('documents','ressources','ressource_documents','ressource_experience','ressource_desires','ressource_interviews');
		$changetype = array('create','update');

		$fragments[] = new Fox\Dao\SqlFragment('handlerkey IN (?) ',
												array($handlerkey),
												array(Connection::PARAM_STR_ARRAY));

		$query = \Fox\Dao::infos(Hive\Ressource::className())->handle()->createQueryBuilder();
		$query->select('handlerkey','handlerid','changetype','author','date')
				->from('versions','r')
				->orderBy('date')
				->orderBy('author')
				->orderBy('handlerkey');


		$fragment = Fox\Dao\SqlFragment::combine($fragments);
		$result = $fragment->pushIn($query)->execute()->fetchAll(\PDO::FETCH_ASSOC);

		$grid = new \Fox\Grid\Tree('gResult');
		$grid->setExpandLevel();
		$grid->fieldcat(array('usrnm'));
		$startDate = !empty($inputs['date_left']) ? new DateTime($inputs['date_left']) : null;
		$endDate = !empty($inputs['date_right']) ? new DateTime($inputs['date_right']) : null;
		$root = new Fox\Node(Fox\Node::ROOT_NODE);
		foreach($result as $data)
		{
			$author = $data['author'];
			$id 	= $data['handlerid'];
			$type = $data['handlerkey'];
			$changeType = $data['changetype']; 
			$bufferDate = new DateTime($data['date']);
			if($bufferDate < $startDate || empty($startDate) )
				$startDate = $bufferDate;
			if($bufferDate > $endDate || empty($endDate) )
				$endDate = $bufferDate;

			// Compute Result for each cells
			if(!isset($authors[$author])) {
				$authors[$author] = $root->add(new Fox\Node(array('usrnm' => User::fullName($author))));
			}
			// Add Nb of Created ressources
			$typeId = 'nbRessourceCreated';
			if(!isset($final[$author][$typeId])) {
				$counterLabel = Msg(Hive::BUNDLE,Hive::REPORT_NB_CREATED_CONTACT);
				$final[$author][$typeId] = $authors[$author]->add(new Fox\Node(array('author' => 1,'usrnm' => $counterLabel)));
			}	
			// Add Nb of Updated Ressources
			$typeId = 'nbRessourceUpdated';
			if(!isset($final[$author][$typeId])) {
				$counterLabel = Msg(Hive::BUNDLE,Hive::REPORT_NB_UPDATED_CONTACT);
				$final[$author][$typeId] = $authors[$author]->add(new Fox\Node(array('author' => 1,'usrnm' => $counterLabel)));
			}
			// Number of distinct created or updated contact
			if($type == 'ressources' || 
				$type == 'ressource_experience' || 
				$type == 'ressource_interviews' || 
				$type == 'ressource_desires' ) {

				if(!isset($contactsUpdated[$author][$id])) {
					$contactsUpdated[$author][$id] = $bufferDate->format($formatType);
				}
				if($changeType == 'create') {
					if(!isset($contactsCreation[$author][$id])) {
						$contactsCreation[$author][$id] = $bufferDate->format($formatType);
					}
				}
				continue;
			}
			
			if(!isset($final[$author][$type]))
			{
				$typeLabel = $type;
				// Handling documents
				if($type == 'documents') {
					$typeLabel = Msg(Hive::BUNDLE,Hive::REPORT_NB_UPLOADED_RESUME);
				} else {
					
				}
				$final[$author][$type] = $authors[$author]->add(new Fox\Node(array('author' => 1,'usrnm' => $typeLabel)));
			}
			$final[$author][$type]->compute($bufferDate->format($formatType),1);
			$final[$author][$type]->compute('total',1);
				
		}
		
		// Combine data to build final summary 
		if(isset($contactsCreation)) {
			foreach($contactsCreation as $author => $data) {
				$type = 'nbRessourceCreated';
					foreach($data as $formatedDate) {
						$final[$author][$type]->compute($formatedDate,1);
						$final[$author][$type]->compute('total',1);
				}
			}
		}
		
		// Combine data to build final summary : Created contact
		if(isset($contactsUpdated)) {
			foreach($contactsUpdated as $author => $data) {
				$type = 'nbRessourceUpdated';
				foreach($data as $formatedDate) {
					$final[$author][$type]->compute($formatedDate,1);
					$final[$author][$type]->compute('total',1);
				}
			}
		}
		if($endDate < $startDate)
		{
			Fox\Message::Error('Error');
		}
		else
		{
			// Build Grid Columns
			while($startDate <= $endDate)
			{
				$iname = $startDate->format($formatType);
				$field = new Field('count',$iname);
				$field->align('left')
					->label($iname);
				$grid->fieldcat()->add($field);
				$startDate->add(new DateInterval($formatInterval));
			}
			
			$grid->fieldcat()->add(new Field('total'));
			$grid->fieldcat()->field('total')->formater(new Number());
		}
		$grid->initialize($root,true);
	}
}
echo $screen->display();
if(isset($grid))
	echo $grid->display();